<template>
  <div class="container">
    <el-form :inline="true">
      <el-form-item>
        <el-input
          v-model="pageGrid.custOrderNo"
          placeholder="客户订单"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="pageGrid.orderNo"
          placeholder="内部订单号"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="pageGrid.custCompany"
          placeholder="公司名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="pageGrid.customer" placeholder="客户名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="pageGrid.date"
          type="daterange"
          range-separator="至"
          @change="setDate"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-select v-model="pageGrid.status" placeholder="请选择状态">
          <el-option label="待审核" value="0"></el-option>
          <el-option label="审核中" value="1"></el-option>
          <el-option label="审批拒绝" value="2"></el-option>
          <el-option label="流转中" value="3"></el-option>
          <el-option label="已完成" value="4"></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item>
        <el-select v-model="pageGrid.param" placeholder="请选择待参数">
          <el-option label="待技术参数" value="技术"></el-option>
          <el-option label="待工艺参数" value="生产课"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <div class="d-flex">
          <el-button type="primary" @click="query">搜索</el-button>
          <el-button @click="clearSearch">重置</el-button>
          <el-upload
            class="upload-demo"
            :action="exportUrl"
            :data="{ type: 'N' }"
            :limit="1"
            :file-list="fileList"
            :on-success="successUpload"
            :show-file-list="false"
          >
            <el-button size="small" type="primary">新品订单导入</el-button>
          </el-upload>
          <el-upload
            class="upload-demo"
            :action="exportUrl"
            :data="{ type: 'U' }"
            :limit="1"
            :file-list="fileList"
            :on-success="successUpload"
            :show-file-list="false"
          >
            <el-button size="small" type="primary">修理订单导入</el-button>
          </el-upload>
        </div>
      </el-form-item>
    </el-form>
    <el-table v-loading="isLoading" :data="tableData" border class="table">
      <el-table-column
        prop="custOrderNo"
        label="客户订单号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="orderNo"
        label="内部订单号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="custCompany"
        label="公司名称"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="customer"
        label="客户"
        align="center"
      ></el-table-column>

      <el-table-column label="订单类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.type == 'U'">修理品订单</span>
          <span v-else>新品订单</span>
        </template>
      </el-table-column>

      <el-table-column
        prop="material"
        label="伸线材质"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="orderDate"
        label="订单日期"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="productionDate"
        label="材料齐备日期"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="requireDate"
        label="要求交期"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="createTime"
        label="导单日期"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="checkRecords"
        label="审核记录"
        align="center"
      ></el-table-column>
      <el-table-column label="订单状态" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.statusStr }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="220">
        <template slot-scope="scope">
          <el-button type="text" @click="goOrderDetail(scope.row)"
            >订单详情</el-button
          >
          <el-button type="text" @click="goFlowDetail(scope.row)"
            >工艺详情</el-button
          >
          <el-button
            type="text"
            @click="showDialog(scope.row)"
            v-if="
              (isQC ||
                isPlan ||
                isTechnology ||
                isProduction ||
                isProduction) &&
              scope.row.checkFlag == '1'
            "
            >审批</el-button
          >
          <el-button type="text" @click="exportExcel(scope.row)"
            >导出</el-button
          >
          <el-button
            type="text"
            @click="deleteWorkDone(scope.row)"
            v-if="isAdmin"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageGrid.pageNo"
        :page-sizes="pageSizeArr"
        :page-size="pageGrid.pageSize"
        layout="total, sizes, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog title="审批" :visible.sync="dialogVisible" width="600px">
      <el-form :model="examine" label-width="80px">
        <el-form-item label="审批状态">
          <el-select v-model="examine.status" placeholder="请选择审批状态">
            <el-option label="通过" value="3"></el-option>
            <el-option label="拒绝" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="订单参数"
          v-show="examine.status == '3' && isProduction"
        >
          <div class="d-flex">
            <el-input
              v-model="examine.parameterUrl"
              readonly
              style="width: 320px"
            ></el-input>
            <el-upload
              class="upload-demo"
              :action="exportUrl"
              :data="parameter"
              :limit="1"
              :file-list="fileList"
              :on-success="examineSuccessUpload"
              :show-file-list="false"
            >
              <el-button size="small" type="primary">文件选择</el-button>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item label="拒绝原因" v-show="examine.status == '2'">
          <el-select v-model="examine.reason" placeholder="请选择拒绝原因">
            <el-option
              :label="item.label"
              :value="item.value"
              v-for="item in rejectReason"
              :key="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import api from "../../../api";
import commonInfo from "@/utils/common";
import host from "@/utils/host";
import { RoleType, OrderRejectReason } from "../../common/enum";
import { exportInfo } from "@/utils/export";
// import { use } from "vue/types/umd";

export default {
  name: "orderList",
  data() {
    return {
      pageSizeArr: commonInfo.pageSizeArr,
      isLoading: false,
      rejectReason: [],
      tableData: [],
      total: 0,
      dialogVisible: false,
      pageGrid: {
        pageNo: 1,
        pageSize: 10,
        custCompany: null,
        custOrderNo: null,
        customer: null,
        startTime: null,
        endTime: null,
        orderNo: null,
        status: null,
        date: null,
        param: null,
      },
      examine: {
        status: "",
        reason: "",
        parameterUrl: "",
        orderNo: "",
      },
      exportLoading: false,
      exportUrl: host.host + "file/open/fileUpload",
      exportExcelUrl: host.host + "excel/open/orderExport",
      uploadData: {
        type: "workDone",
      },
      parameter: {
        type: "parameter",
      },
      statusObj: {
        0: "待审核",
        1: "审核中",
        2: "审批拒绝",
        3: "流转中",
        4: "已完成",
      },
      orderNo: null,
      fileList: [],
      isAdmin: false,
      isBusiness: false, // 业务
      isProduction: false,
      isQC: false, //
      isPlan: false, //
      isTechnology: false, //
    };
  },
  created() {
    this.getData();
    let user = sessionStorage.getItem("adminUserInfo");
    if (user) {
      user = JSON.parse(user);
      this.isBusiness = user.role === RoleType.Business;
      this.isQC = user.role === RoleType.QC;
      this.isProduction = user.role === RoleType.Production;
      this.isPlan = user.role === RoleType.Plan;
      this.isTechnology = user.role === RoleType.Technology;
      this.isAdmin = user.role === RoleType.Admin;
    }
    this.rejectReason = OrderRejectReason;
  },
  methods: {

   query(){
 this.pageGrid.pageNo=1;
 this.getData();
   },

    getData() {
      this.isLoading = true;
      api
        .getInsideOrderList(this.pageGrid)
        .then((res) => {
          if (res.code === 200) {
            this.tableData = res.totalElement;
            this.total = res.total;
            this.isLoading = false;
          }
        })
        .catch((this.isLoading = false));
    },
    setDate(val) {
      if (val) {
        this.pageGrid.startTime = val[0];
        this.pageGrid.endTime = val[1];
      } else {
        this.pageGrid.startTime = null;
        this.pageGrid.endTime = null;
      }
    },
    goOrderDetail(row) {
      this.$router.push({
        path: "orderDetail",
        query: {
          id: row.orderNo,
        },
      });
    },
    goFlowDetail(row) {
      //跳到修理品工艺详情页，
      if (row.type == "U") {
        this.$router.push({
          path: "updateFlowDetail",
          query: {
            id: row.orderNo,
          },
        });
      } else {
        this.$router.push({
          path: "flowDetail",
          query: {
            id: row.orderNo,
          },
        });
      }
    },
    exportExcel(row) {
      this.exportLoading = true;
      let info = {
        method: "GET",
        url: this.exportExcelUrl,
        responseType: "blob",
        type: "excel",
        name: "订单信息",
        params: {
          orderNo: row.orderNo,
        },
      };
      exportInfo(info).then((res) => {
        this.exportLoading = false;
      });
    },
    successUpload(response, file, fileList) {
      this.fileList = [];
      api.importOrderAdd({ url: response.data }).then((res) => {
        this.getData();
        this.$message.success("导入成功");
      });
    },
    examineSuccessUpload(response) {
      if (response.code == 200) {
        this.fileList = [];
        this.examine.parameterUrl = response.data;
        this.$message.success("导入成功");
      } else {
        this.$message.error(response.message);
      }
    },
    showDialog(row) {
      this.orderNo = row.orderNo;
      this.examine = {
        status: "",
        reason: "",
        parameterUrl: "",
        orderNo: row.orderNo,
      };
      this.fileList = [];
      this.dialogVisible = true;
    },
    deleteWorkDone(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          api.insideOrderDelete({ orderNo: row.orderNo }).then((res) => {
            if (res.code == 200) {
              this.$message.success("删除成功");
              this.pageGrid.pageNo = 1;
              this.getData();
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {});
    },
    submit() {
      if (!this.examine.status) return false;
      if (this.examine.status == "2" && !this.examine.reason) {
        return this.$message.error("请先选择完整");
      }
      if (
        this.examine.status == "3" &&
        !this.examine.parameterUrl &&
        this.isProduction
      ) {
        return this.$message.error("请先选择完整");
      }
      api.insideOrderCheck(this.examine).then((res) => {
        if (res.code == 200) {
          this.dialogVisible = false;
          this.$message.success("操作成功");
          this.pageGrid.pageNo = 1;
          this.getData();
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    clearSearch() {
      this.pageGrid = {
        pageNo: 1,
        pageSize: 10,
        parameter: {
          orderNo: null,
          account: null,
        },
      };
      this.getData();
    },
    //分页
    handleCurrentChange(val) {
      this.pageGrid.pageNo = val;
      this.getData();
    },
    //分页
    handleSizeChange(val) {
      this.pageGrid.pageSize = val;
      this.getData();
    },
  },
};
</script>

<style scoped lang="less">
.upload-demo {
  height: 32px;
  display: inline-block;
  margin-left: 10px;
}
</style>
